/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */
#include "Alarm.h"
#include "base/util/StringBuffer.h"
#include "base/Log.h"


bool isUTCFormat(const char* d) {
    return strstr(d, "Z") ? true : false;
}

/**
 * It calculates the number of seconds before the event starts. It is used for the alarm time.
 * It returns a negative value because it is before...
 */

long calculateReminderSeconds(Date& start, Date& alarm) {
    
    NSDate* begin =    getAppleDateValue(start);
    NSDate* reminder = getAppleDateValue(alarm); 
    
    NSTimeInterval time1 = [begin timeIntervalSinceReferenceDate];
    NSTimeInterval time2 = [reminder timeIntervalSinceReferenceDate];
    
    long secs = 0;
    if (time1 > time2) {
        secs = time1 - time2;
    } else {
        secs = time1 - time1;
    }
    [begin release];
    [reminder release];
    
    return -(long)secs;
}


StringBuffer formatReminderDate(Date& start, long secondBeforeStart) {
    
    NSDate* begin = getAppleDateValue(start);
    
    NSTimeInterval time = [begin timeIntervalSinceReferenceDate];
    
    if (secondBeforeStart < 0) {
        time += secondBeforeStart; // it is a less time
    } else {
        time -= secondBeforeStart;
    }
    
    StringBuffer ret = convertAppleDateToStandard(time, false, isUTCFormat(start.getDate()));
    return ret;
    
}



void Alarm::setPrimaryAlarm(Date& d) { 
    primaryAlarm = d; 
    primary_minBeforeStart = 0;
}

void Alarm::setSecondaryAlarm(Date& d) { 
    secondaryAlarm = d; 
    secondary_minBeforeStart = 0;
}


StringBuffer getStringValue(long value, bool minutes) {
    StringBuffer ret;
    long val = (minutes ? value : (value*60));
    ret.sprintf("%ld", val);
    LOG.debug("%s beforestart %s", (minutes ? "Minutes" : "Seconds" ), ret.c_str());
    return ret;
}


StringBuffer Alarm::getPrimary_minBeforeStart(void*) {
    return getStringValue(primary_minBeforeStart, true);
}
StringBuffer Alarm::getPrimary_secBeforeStart(void*) {
    return getStringValue(primary_minBeforeStart, false);
}

StringBuffer Alarm::getSecondary_minBeforeStart(void*) {
    return getStringValue(secondary_minBeforeStart, true);
}
StringBuffer Alarm::getSecondary_secBeforeStart(void*) {
     return getStringValue(secondary_minBeforeStart, false);
}

void Alarm::completeFields(Date& startDate) {
    
    bool conversionDone = false;
    
    if (primary_minBeforeStart == 0) {
        primary_minBeforeStart = calculateReminderSeconds(startDate, primaryAlarm);
        primary_minBeforeStart = primary_minBeforeStart / 60;
        conversionDone = true;
    }
    if (secondary_minBeforeStart == 0) {
        secondary_minBeforeStart = calculateReminderSeconds(startDate, primaryAlarm);
        secondary_minBeforeStart = secondary_minBeforeStart / 60;
        conversionDone = true;
    }
    
    if (conversionDone) {
        return;
    }
    
    if (primary_minBeforeStart != UNUSED_LONG_VALUE) {
        StringBuffer ret = formatReminderDate(startDate, getPrimary_secBeforeStart());
        Date d; d.setDate(ret);
        setPrimaryAlarm(d);
    }
    if (secondary_minBeforeStart != UNUSED_LONG_VALUE) {
        StringBuffer ret = formatReminderDate(startDate, getSecondary_secBeforeStart());
        Date d; d.setDate(ret);
        setSecondaryAlarm(d);
    }
    return;
    
}

